//
//  UIView+Coord.m
//  LQCommonTools
//
//  Created by Fun Tech on 2017/10/30.
//  Copyright © 2017年 Leslie. All rights reserved.
//

#import "UIView+Coord.h"

@implementation UIView (Coord)

// x
- (CGFloat)v_x{
    return self.frame.origin.x;
}

- (void)setV_x:(CGFloat)v_x{
    self.frame = CGRectMake(v_x, self.v_y, self.v_w, self.v_h);
}

// y
- (CGFloat)v_y{
    return self.frame.origin.y;
}

- (void)setV_y:(CGFloat)v_y{
    self.frame = CGRectMake(self.v_x, v_y, self.v_w, self.v_h);
}

// h
- (CGFloat)v_h{
    return self.frame.size.height;
}

- (void)setV_h:(CGFloat)v_h{
    self.frame = CGRectMake(self.v_x, self.v_y, self.v_w, v_h);
}

// w
- (CGFloat)v_w{
    return self.frame.size.width;
}

- (void)setV_w:(CGFloat)v_w{
    self.frame = CGRectMake(self.v_x, self.v_y, v_w, self.v_h);
}

// center x
- (CGFloat)v_centerX{
    return self.center.x;
}

- (void)setV_centerX:(CGFloat)v_centerX{
    self.center = CGPointMake(v_centerX, self.v_centerY);
}

// center y
- (CGFloat)v_centerY{
    return self.center.y;
}

- (void)setV_centerY:(CGFloat)v_centerY{
    self.center = CGPointMake(self.v_centerX, v_centerY);
}

// max x
- (CGFloat)v_MaxX{
    return self.v_x + self.v_w;
}

- (void)setV_MaxX:(CGFloat)v_MaxX{
    self.frame = CGRectMake(v_MaxX-self.v_w, self.v_y, self.v_w, self.v_h);
}

// max y
- (CGFloat)v_MaxY{
    return self.v_y + self.v_h;
}

- (void)setV_MaxY:(CGFloat)v_MaxY{
    self.frame = CGRectMake(self.v_x, v_MaxY-self.v_h, self.v_w, self.v_h);
}

/// 设置多个view与调用者底部相等
- (void)j_setBottomsEqualToMe:(NSArray <UIView *> *)views{
    for (UIView *view in views) {
        view.v_MaxY = self.v_MaxY;
    }
}

/// 设置多个view与调用者顶部相等
- (void)j_setTopsEqualToMe:(NSArray <UIView *> *)views{
    for (UIView *view in views) {
        view.v_y = self.v_y;
    }
}

/// 设置多个view与调用者左边相等
- (void)j_setLeftsEqualToMe:(NSArray <UIView *> *)views{
    for (UIView *view in views) {
        view.v_x = self.v_x;
    }
}

/// 设置多个view与调用者右边相等
- (void)j_setRightsEqualToMe:(NSArray <UIView *> *)views{
    for (UIView *view in views) {
        view.v_MaxX = self.v_MaxX;
    }
}

/// 设置多个view与调用者水平中心点相等
- (void)j_setHorizontalsEqualToMe:(NSArray <UIView *> *)views{
    for (UIView *view in views) {
        view.v_centerX = self.v_centerX;
    }
}

/// 设置多个view与调用者垂直中心点相等
- (void)j_setVerticalsEqualToMe:(NSArray <UIView *> *)views{
    for (UIView *view in views) {
        view.v_centerY = self.v_centerY;
    }
}

/// 设置view基于父视图垂直居中
- (void)j_fitVerticalCenterInView:(UIView *)view{
    self.v_y = (view.v_h - self.v_h)/2;
}

/// 设置view基于父视图水平居中
- (void)j_fitHorizontalCenterInView:(UIView *)view{
    self.v_x = (view.v_w - self.v_w)/2;
}


- (void)j_setAnchorPoint:(CGPoint)anchorPoint{
    
    CGPoint oldOrigin = self.frame.origin;
    self.layer.anchorPoint = anchorPoint;
    CGPoint newOrigin = self.frame.origin;
    
    CGPoint transition;
    transition.x = newOrigin.x - oldOrigin.x;
    transition.y = newOrigin.y - oldOrigin.y;
    
    self.center = CGPointMake (self.center.x - transition.x, self.center.y - transition.y);
}

@end
